/**
 * ...
 * @author Thot
 */

function MapScreen()
{
	this.signal = new Signal();
}



	// private
	MapScreen.prototype._map				= null;
	MapScreen.prototype._mouseTracker 		= null;
	MapScreen.prototype._keyTracker 		= null;
	MapScreen.prototype._container			= null;
	MapScreen.prototype.signal				= null;
	
	// static
	MapScreen.ADD_ANIM					= "addAnim";
	
	
	
	// handlers
	MapScreen.prototype._dblClickHandler = function()
	{
		//var x = this._mouseTracker.localX + this._map.camera.x;
		//var y = this._mouseTracker.localY + this._map.camera.y;
		//this.signal.dispatchEvent( MapScreen.ADD_ANIM, [{"x":x,"y":y}] );
	};
	
	//MapScreen.prototype._upHandler = function(){};
	//MapScreen.prototype._downHandler = function(){};
	
	MapScreen.prototype._setEventListeners = function()
	{		
		 //initialize the mouse/touch tracker
		this._mouseTracker = new MouseTracker(this._container);
		
		//this._mouseTracker.signal.addListener( MouseTracker.MOUSE_UP, this._upHandler, this );
		//this._mouseTracker.signal.addListener( MouseTracker.MOUSE_DOWN, this._downHandler, this );
		this._mouseTracker.signal.addListener( MouseTracker.DOUBLE_CLICK, this._dblClickHandler, this );
		
		this._keyTracker = new KeyboardTracker();
		this._keyTracker.listen(this._container);
	};
	
	
	
	// public
	MapScreen.prototype.init = function( p_container, p_camW, p_camH )
	{
		this._map = new Map();
		this._container = p_container;
		this._map.init( p_camW, p_camH );
		this._setEventListeners();
		this.resume();
	};
	
	
	MapScreen.prototype.zoomAt = function( p_x, p_y, p_zoom )
	{
		var zoom 	= p_zoom;
		var pivotX 	= p_x;
		var pivotY 	= p_y;
		
		this._map.camera.pivotX = pivotX;
		this._map.camera.pivotY = pivotY;
		this._map.camera.zoom 	= zoom;
		
		//TweenNano.to( this._map.camera, 500, {"zoom": zoom} );
	};
	
	
	MapScreen.prototype.addSpriteAt = function( p_animVO, p_layerId, p_x, p_y )
	{
		var anim = AnimationFactory.getAnimationFromVO( p_animVO );
		var spr = new Sprite();
		
		spr.width 		= p_animVO.width;
		spr.height 		= p_animVO.height;
		spr.uv 			= anim.data[0].uv;
		spr.textureId 	= anim.data[0].tex;
		
		anim.sprite = spr;
		
		if( anim.data.length > 0 )
		{
			var rand = ( Math.random() * 1000000 ) >> 0;
			AnimationManager.getInstance().registerAnimation( anim, anim.name+"_"+rand );
		}
		
		this._map.addSpriteAt( spr, p_layerId, p_x, p_y );
	};
	
	
	MapScreen.prototype.addLayer = function( p_layerId )
	{
		var layer = this._map.addLayer( p_layerId );
		this._container.appendChild( layer.canvas );
	};
	
	
	MapScreen.prototype.removeLayer = function( p_layerId )
	{
		var layer = this._map.removeLayer( p_layerId );
		if( layer == null )
		{
			return;
		}
			
		this._container.removeChild( layer.canvas );
	};
	
	
	
	// rendering loop
	MapScreen.prototype.render = function()
	{
		var state = this._keyTracker.getKeyState("a");
		AnimationManager.getInstance().animateAll();
		
		if( this._mouseTracker.down )
		{
			if( state == 0 )
			{
				var x = this._mouseTracker.localX + this._map.camera.x;
				var y = this._mouseTracker.localY + this._map.camera.y;
				this.signal.dispatchEvent( MapScreen.ADD_ANIM, [{"x":x,"y":y}] );
			}
			else
			{
				this._map.camera.x -= ( this._mouseTracker.mouseX - this._mouseTracker.lastX );
				this._map.camera.y -= ( this._mouseTracker.mouseY - this._mouseTracker.lastY );
				this._mouseTracker.lastX = this._mouseTracker.mouseX;
				this._mouseTracker.lastY = this._mouseTracker.mouseY;
			}
		}
		
		var time = new Date().getTime();
		this._map.render();
		time = new Date().getTime() - time;
	};
	
	
	MapScreen.prototype.resume = function()
	{
		FrameManager.getInstance().removeFrameListener( this.render, this );
		FrameManager.getInstance().addFrameListener( this.render, this );
	};
	
	
	MapScreen.prototype.pause = function()
	{
		FrameManager.getInstance().removeFrameListener( this.render, this );
	};
	
	
	MapScreen.prototype.clean = function()
	{
		var layers = this._map.getLayers();
		var layer = null;
		var i = 0;
		var max = layers.length;
		
		for( i = 0; i < max; i++ )
		{
			layer = layers[i];
			this._container.removeChild( layer.canvas );
		}
		
		this._map.clean();
		
		AnimationManager.getInstance().destroy();
	};
	

	

	// loading...
	MapScreen.prototype.showProgression = function( p_progression )
	{
		console.log(p_progression+"%");
		//var context = this._layer.context;
		//context.fillStyle = "white";
		//context.fillRect( 0, 100, 100, 20 );
		//context.fillStyle = "#666666";
		//context.font = '15px sans-serif';
		//context.fillText(p_progression+"%",10,100);
	};
	
